package com.taxshare.operation.service;

import static com.taxshare.operation.constant.MinIOConstant.BUCKET_NAME;
import static com.taxshare.operation.constant.MinIOConstant.GROUP_API;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.minio.dto.MinIOUploadResDTO;
import com.taxshare.minio.utils.MinIOUtils;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.mybatis.utils.DozerBeanUtil;
import com.taxshare.operation.entity.ApiEntity;
import com.taxshare.operation.mapper.ApiMapper;
import com.taxshare.operation.view.vo.ApiInfoVO;
import com.taxshare.operation.view.vo.ApiQyztReqVO;
import com.taxshare.response.beans.Response;
import com.taxshare.tools.util.UUIDUitl;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class InterfaceService extends ServiceImpl<ApiMapper, ApiEntity> {

	@Autowired
    private MinIOUtils  minIOUtils;
	
	/**
	 * @Title: queryPageList
	 * @Description: (接口列表分页)
	 * @param @param query
	 * @param @return 参数
	 * @return Response<IPage<ApiInfoVO>> 返回类型
	 * @throws
	 */
	public Response<IPage<ApiInfoVO>> queryPageList(PageRequest<ApiInfoVO> query) {
		QueryWrapper<ApiEntity> queryWrapper = new QueryWrapper<>();
		String jkmc = query.getInfo().getJkmc();
		if (StringUtils.isNotEmpty(jkmc)) {
			queryWrapper.like("jkmc", jkmc);
		}
		String qyzt = query.getInfo().getQyzt();
		if (StringUtils.isNotEmpty(qyzt)) {
			queryWrapper.eq("qyzt", qyzt);
		}
		queryWrapper.orderByAsc("sort");
		queryWrapper.orderByDesc("create_time");
		IPage<ApiInfoVO> result = PageHelp.siamplePage(this, queryWrapper, query.getPage(), ApiInfoVO.class);
		return Response.success(result);
	}

	/**
	 * @Title: queryList
	 * @Description: (接口列表)
	 * @param @param apiInfoVO
	 * @param @return 参数
	 * @return Response<List<ApiInfoVO>> 返回类型
	 * @throws
	 */
	public Response<List<ApiInfoVO>> queryList(ApiInfoVO apiInfoVO) {
		QueryWrapper<ApiEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("create_time");
		queryWrapper.eq("qyzt", "1");
		List<ApiEntity> list = this.list(queryWrapper);
		return Response.success(DozerBeanUtil.mapList(list, ApiInfoVO.class));
	}

	/**
	 * @Title: showApiInfo
	 * @Description: (预览)
	 * @param @param uuid
	 * @param @return 参数
	 * @return Response 返回类型
	 * @throws
	 */
	public Response showApiInfo(String uuid) {
		ApiEntity result = this.getById(uuid);
		ApiInfoVO apiInfoVO = DozerBeanUtil.map(result, ApiInfoVO.class);
		return Response.success(apiInfoVO);
	}

	/**
	 * @Title: updateApiSort
	 * @Description: (更新接口排序字段)
	 * @param @param apiInfoVO
	 * @param @return 参数
	 * @return Response 返回类型
	 * @throws
	 */
	public Response updateApiSort(ApiQyztReqVO apiQyztReqVO) {
		ApiEntity apiEntity = DozerBeanUtil.map(apiQyztReqVO, ApiEntity.class);
		apiEntity.setUpdateTime(new Date());
		return Response.success(this.updateById(apiEntity));
	}

	/**
	 * @Title: add
	 * @Description: (新增接口)
	 * @param @param commonCodeInfoDTO
	 * @param @return 参数
	 * @return Response 返回类型
	 * @throws
	 */
	public Response saveApi(ApiInfoVO apiInfoVO) {
//		List<Map<String, String>> qqcs = apiInfoVO.getQqcs();
//		List<Map<String,String>> fhcs = apiInfoVO.getFhcs();
//		JSONArray qqcsJson =  JSONUtil.parseArray(qqcs);
//		JSONArray fhcsJson =  JSONUtil.parseArray(fhcs);
		ApiEntity apiEntity = DozerBeanUtil.map(apiInfoVO, ApiEntity.class);
//		apiEntity.setQqcs(qqcsJson.toString());
//		apiEntity.setFhcs(fhcsJson.toString());
		apiEntity.setUuid(UUIDUitl.UUID32(false));
		apiEntity.setCreateTime(new Date());
		apiEntity.setUpdateTime(new Date());
		this.save(apiEntity);
		return Response.success();
	}
	

	/**
	 * @Title: udpateApiInfo
	 * @Description: (接口修改)
	 * @param @param apiInfoVO
	 * @param @return 参数
	 * @return Response 返回类型
	 * @throws
	 */
	public Response udpateApiInfo(ApiInfoVO apiInfoVO) {
//		List<Map<String, String>> qqcs = apiInfoVO.getQqcs();
//		List<Map<String,String>> fhcs = apiInfoVO.getFhcs();
//		JSONArray qqcsJson =  JSONUtil.parseArray(qqcs);
//		JSONArray fhcsJson =  JSONUtil.parseArray(fhcs);
		ApiEntity apiEntity = DozerBeanUtil.map(apiInfoVO, ApiEntity.class);
//		apiEntity.setQqcs(qqcsJson.toString());
//		apiEntity.setFhcs(fhcsJson.toString());
		apiEntity.setUpdateTime(new Date());
		return Response.success(this.updateById(apiEntity));
	}

	/**
	 * @Title: delete
	 * @Description: (接口删除)
	 * @param @param ids
	 * @param @return 参数
	 * @return Response 返回类型
	 * @throws
	 */
	public Response deleteApiInfo(String uuid) {
		return Response.success(this.removeById(uuid));
	}

	/**
	 * @Title: uploadFile
	 * @Description: (文件上传)
	 * @param @param file
	 * @param @param uuid
	 * @param @return 参数
	 * @return Response 返回类型
	 * @throws
	 */
	public Response uploadFile(MultipartFile file, String uuid) {
		String fileName = file.getOriginalFilename();
		MinIOUploadResDTO minIOUploadResDTO = minIOUtils.upload(BUCKET_NAME, GROUP_API, file, false);
		ApiEntity document = this.getById(uuid);
		document.setIsUpload("1");
		document.setFileName(minIOUploadResDTO.getMinFileName());
		document.setFileUrl(minIOUploadResDTO.getMinFileUrl().split("\\?")[0]);
		document.setUpdateTime(new Date());
		this.updateById(document);
		return Response.success();
	}

	/**
	 * @Title: down
	 * @Description: (文件下载)
	 * @param @param response
	 * @param @param uuid 参数
	 * @return void 返回类型
	 * @throws
	 */
	public void down(HttpServletResponse response, String uuid) {
		ApiEntity document = this.getById(uuid);
		minIOUtils.download(response, BUCKET_NAME, GROUP_API, document.getFileName());
	}
}
